<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @enter="handleQuery"
        label-width="63px"
      >
        <el-form-item label="IP地址" prop="ipAddress" label-width="50px">
          <common-input
            v-model="queryParams.ipAddress"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="所属城市" prop="cityName">
          <el-cascader
            style="width: 100%"
            v-model="queryParams.cityName"
            :options="allCityList"
            @change="handleChange"
            :props="{
              value: 'name',
              label: 'name',
              emitPath: false,
            }"
            :show-all-levels="false"
          >
            <template slot-scope="{ node, data }">
              <common-carousel-text :content="data.name"></common-carousel-text>
            </template>
          </el-cascader>
        </el-form-item>
      </el-form>
    </common-search>

    <el-row :gutter="10" class="mb8" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleEdit"
          data-test="rule_variable-button-add"
          v-hasPermi="['analysiscenter:library:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['analysiscenter:library:import']"
          type="primary"
          plain
          icon="el-icon-download"
          @click="importShow"
          >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['analysiscenter:library:export']"
          type="primary"
          plain
          icon="el-icon-upload2"
          :loading="exportLoading"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="ids.length < 1"
          @click="handleDelete"
          v-hasPermi="['analysiscenter:library:remove']"
          >批量删除
        </el-button>
      </el-col>
    </el-row>

    <common-table
      v-loading="loading"
      slot="content"
      :data="ipLibList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="IP地址"
        align="left"
        prop="ipAddress"
        show-overflow-tooltip
      />
      <el-table-column
        label="所属城市"
        align="left"
        prop="cityName"
        show-overflow-tooltip
      />
      <el-table-column
        label="城市经度"
        align="left"
        prop="cityLongitude"
        show-overflow-tooltip
      />
      <el-table-column
        label="城市纬度"
        align="left"
        prop="cityLatitude"
        show-overflow-tooltip
      />

      <el-table-column label="操作" align="left" width="100px">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['analysiscenter:library:edit']"
            >编辑
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['analysiscenter:library:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <ip-add ref="ipAdd" @submitForm="submitForm"></ip-add>

    <common-upload-dialog
      download-path="system/alarm/code/downFileByemplateName"
      upload-path="/analysiscenter/ipAddress/library/importData"
      template-name="ipAddressTemplate.xlsx"
      report-path="analysiscenter/ipAddress/library/downErrorReportByemplateName"
      ref="uploadDialog"
      :getList="getList"
    />
  </common-list>
</template>

<script>
import {
  fetchList,
  ipLibAdd,
  ipLibEdit,
  ipLibDelete,
} from "@/api/analysisCenter/ipAddr";
import IpAdd from "./components/ipAdd.vue";
import BaseList from "@/mixins/BaseList";
import ALLCITY from "@/utils/city";

export default {
  mixins: [BaseList],
  components: {
    IpAdd,
  },
  data() {
    return {
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipAddress: null,
        cityName: null,
      },
      total: 0,
      ipLibList: [],
      loading: false,
      exportLoading: false,
      allCityList: ALLCITY,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      fetchList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.ipLibList = res.rows;
            this.total = res.total;
          } else {
            this.ipLibList = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch(() => {
          this.ipLibList = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    handleExport() {
      //导出
      this.exportLoading = true;
      this.download(
        "analysiscenter/ipAddress/library/export",
        {
          ...this.query,
          pageSize: 10000,
        },
        `ipLibTemplate_${new Date().getTime()}.xlsx`
      ).finally(() => {
        this.exportLoading = false;
      });
    },

    importShow() {
      //导入
      this.$refs.uploadDialog.show();
    },

    handleChange(data) {},

    handleEdit(row) {
      this.$refs.ipAdd.show(row ? { ...row } : {});
    },

    handleView(row) {
      this.$refs.detail.show(row);
    },

    handleDelete(row) {
      const ids = row.id || this.ids.join(",");
      this.$GlobalDeleteConfirm()
        .then(() => {
          ipLibDelete({ ids }).then((res) => {
            if (res && res.code == 200) {
              this.$GlobalSuccessMessage("删除成功");
              if (
                this.queryParams.pageNum > 1 &&
                ids.split(",").length === this.ipLibList.length
              ) {
                this.queryParams.pageNum -= 1;
              }
              this.getList();
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg);
            }
          });
        })
        .catch(() => {});
    },

    submitForm() {
      this.getList();
    },

    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
